import { Logger, Provide } from '@midwayjs/core';
import { BaseService } from '@cool-midway/core';
import { ILogger } from '@midwayjs/logger';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { DeviceDeviceEntity } from '../../device/entity/device';
/**
 * 描述
 */
@Provide()
export class TaskDemoService extends BaseService {
  @Logger()
  logger: ILogger;

  @InjectEntityModel(DeviceDeviceEntity)
  DeviceDeviceEntity: Repository<DeviceDeviceEntity>;
  /**
   * 描述
   */
  async test(a, b) {
    this.logger.info('我被调用了', a, b);
    return '任务执行成功';
  }

  async exec() {
    /* 遍历device表，判断最后在线时间是否超过120秒，如果超出，则修改其状态为2 */
    const devices = await this.DeviceDeviceEntity.find();

    for (const device of devices) {
      if (
        device.lastOnlineTime &&
        device.lastOnlineTime + 120 < Date.now() / 1000
      ) {
        device.deviceStatus = 2;
        await this.DeviceDeviceEntity.save(device);
      }
    }
  }
}
